--[[	$Id: actionBars.lua 2544 2012-09-16 10:59:40Z sdkyron@gmail.com $	]]

local kill, pixelScale = caelLib.kill, caelLib.scale

---------------------------------------------
-- Local variable settings for action bars.
---------------------------------------------

local actionBar = {
	["settings"] = {
		["showGrid"] = true,
		["showPetGrid"] = false,
		["mouseOverBar1"] = false,
		["mouseOverBar2"] = false,
		["mouseOverBar3"] = false,
		["mouseOverBar4"] = false,
		["mouseOverBar5"] = true,
		["mouseOverPetBar"] = true,
		["mouseOverShapeshiftBar"] = false,
		["showBar1"] = true,
		["showBar2"] = true,
		["showBar3"] = true,
		["showBar4"] = true,
		["showBar5"] = false,
	},
}

-- Global variable
local _G = getfenv(0)

-----------------------------------------------
-- Hide default Blizzard frames we don't need
-----------------------------------------------

do
	MainMenuBar:SetScale(0.00001)
	MainMenuBar:EnableMouse(false)
	OverrideActionBar:SetScale(0.00001)
	OverrideActionBar:EnableMouse(false)
	PetActionBarFrame:EnableMouse(false)
	StanceBarFrame:EnableMouse(false)

	-- Frame List
	local elements = {
		MainMenuBar, MainMenuBarArtFrame, OverrideActionBar,
		PossessBarFrame, PetActionBarFrame,
	}

	for _, element in pairs(elements) do

		if element:GetObjectType() == "Frame" then
			element:UnregisterAllEvents()
		end
--[[
		if element == MainMenuBar then
			element:EnableMouse(false)
			element:SetScale(0.01)
			element:SetAlpha(0)
		else
			hooksecurefunc(element, "Show", function(self)
				self:Hide()
			end)
		end
--]]
		if element ~= MainMenuBar then
			element:Hide()
		end
		element:SetAlpha(0)

	end

	elements = nil

	IconIntroTracker:UnregisterAllEvents()
	IconIntroTracker:Hide()
	
	-- UI Parent Manager frame nil'ing

	-- Frame List
	local uiManagedFrames = {
		"MultiBarLeft", "MultiBarRight", "MultiBarBottomLeft", "MultiBarBottomRight",
		"StanceBarFrame", "PossessBarFrame", "PETACTIONBAR_YPOS",
		"MultiCastActionBarFrame", "MULTICASTACTIONBAR_YPOS", "ChatFrame1",	"ChatFrame2",
	}

	for _, frame in pairs(uiManagedFrames) do
		UIPARENT_MANAGED_FRAME_POSITIONS[frame] = nil
	end

	uiManagedFrames = nil

	hooksecurefunc("TalentFrame_LoadUI", function()
		PlayerTalentFrame:UnregisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
	end)
end

---------------------------------
-- Toggle for mouseover on bars
---------------------------------

local function mouseOverBar(panel, bar, button, alpha)
	if bar ~= nil then
		bar:SetAlpha(alpha)
	end

	if panel ~= nil then
		panel:SetAlpha(alpha)
	end

	if button ~= nil then
		for index = 1, 12 do
			_G[button .. index]:SetAlpha(alpha)
		end
	end
end

----------------------
-- Setup button grid
----------------------

local buttonGrid = CreateFrame("Frame")
buttonGrid:RegisterEvent("PLAYER_ENTERING_WORLD")
buttonGrid:SetScript("OnEvent", function(self, event)
	self:UnregisterEvent("PLAYER_ENTERING_WORLD")
	SetActionBarToggles(actionBar["settings"].showBar2, actionBar["settings"].showBar3, actionBar["settings"].showBar4, actionBar["settings"].showBar5)

	if actionBar["settings"].showGrid == true then
		for index = 1, 12 do
			local button = _G[format("ActionButton%d", index)]
			button:SetAttribute("showgrid", 1)
			ActionButton_ShowGrid(button)
--[[
			button = _G[format("BonusActionButton%d", index)]
			button:SetAttribute("showgrid", 1)
			ActionButton_ShowGrid(button)
--]]
			button = _G[format("MultiBarRightButton%d", index)]
			button:SetAttribute("showgrid", 1)
			ActionButton_ShowGrid(button)

			button = _G[format("MultiBarBottomRightButton%d", index)]
			button:SetAttribute("showgrid", 1)
			ActionButton_ShowGrid(button)

			button = _G[format("MultiBarLeftButton%d", index)]
			button:SetAttribute("showgrid", 1)
			ActionButton_ShowGrid(button)

			button = _G[format("MultiBarBottomLeftButton%d", index)]
			button:SetAttribute("showgrid", 1)
			ActionButton_ShowGrid(button)
		end
	end
end)

---------------------------------------------------
-- BAR 1
---------------------------------------------------

local bar1 = CreateFrame("Frame", "bar1", caelPanel5, "SecureHandlerStateTemplate")
bar1:ClearAllPoints()
bar1:SetAllPoints(caelPanel5)

local barPage = {
	["DRUID"] = "[bonusbar:1,nostealth] 7; [bonusbar:1,stealth] 8; [bonusbar:2] 8; [bonusbar:3] 9; [bonusbar:4] 10;",
	["WARRIOR"] = "[bonusbar:1] 7; [bonusbar:2] 8; [bonusbar:3] 9;",
	["PRIEST"] = "[bonusbar:1] 7;",
	["MONK"] = "[bonusbar:1] 7; [bonusbar:2] 8; [bonusbar:3] 9;",
	["ROGUE"] = "[bonusbar:1] 7; [form:3] 7;",
	["WARLOCK"] = "[form:2] 10;",
	["DEFAULT"] = "[vehicleui:12] 12; [possessbar] 12; [overridebar] 14; [bar:2] 2; [bar:3] 3; [bar:4] 4; [bar:5] 5; [bar:6] 6;",
}

local function GetBar()
	local condition = barPage["DEFAULT"]
	local class = caelLib.playerClass
	local page = barPage[class]

	if page then
		condition = condition.." "..page
	end

	condition = condition.." 1"

	return condition
end

bar1:RegisterEvent("PLAYER_LOGIN")
bar1:RegisterEvent("PLAYER_ENTERING_WORLD")
bar1:RegisterEvent("KNOWN_CURRENCY_TYPES_UPDATE")
bar1:RegisterEvent("CURRENCY_DISPLAY_UPDATE")
bar1:RegisterEvent("BAG_UPDATE")
bar1:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
bar1:SetScript("OnEvent", function(self, event, ...)
	if event == "PLAYER_LOGIN" or event == "ACTIVE_TALENT_GROUP_CHANGED" then
		local button
		for i = 1, NUM_ACTIONBAR_BUTTONS do
			button = _G["ActionButton"..i]
			self:SetFrameRef("ActionButton"..i, button)
		end	

		self:Execute([[
			buttons = table.new()
			for i = 1, 12 do
				table.insert(buttons, self:GetFrameRef("ActionButton"..i))
			end
		]])

		self:SetAttribute("_onstate-page", [[ 
			for i, button in ipairs(buttons) do
				button:SetAttribute("actionpage", tonumber(newstate))
			end
		]])

		RegisterStateDriver(self, "page", GetBar())
	elseif event == "PLAYER_ENTERING_WORLD" then
		local button
		for i = 1, 12 do
			button = _G["ActionButton"..i]
			button:SetScale(0.68) -- 0.68625
			button:ClearAllPoints()
			button:SetParent(self)
			if i == 1 then
				button:SetPoint("TOPLEFT", caelPanel5, pixelScale(4.5), pixelScale(-4.5))
			elseif i == 7 then
				button:SetPoint("TOPLEFT", _G["ActionButton1"], "BOTTOMLEFT", 0, pixelScale(-6.5))
			else
				button:SetPoint("LEFT", _G["ActionButton"..i-1], "RIGHT", pixelScale(5), 0)
			end
		end
	else
		MainMenuBar_OnEvent(self, event, ...)
	end
end)

------------------------
-- BAR 2, BAR 3, BAR 4
------------------------

-- Bar2 = caelPanel6 = MultiBarBottomLeft
-- Bar3 = caelPanel7 = MultiBarBottomRight
-- Bar4 = caelPanel4 = MultiBarRight
-- Bar5 = caelPanel<NOT MADE> = MultiBarLeft

local bar2 = CreateFrame("Frame", "bar2", UIParent)
local bar3 = CreateFrame("Frame", "bar3", UIParent)
local bar4 = CreateFrame("Frame", "bar4", UIParent)
local bar5 = CreateFrame("Frame", "bar5", UIParent)

do 
	local bars = {{}, {}, {}}

	bars[1] = {
		["panel"] = caelPanel6,
		["button"] = "MultiBarBottomLeftButton",
		["barFrame"] = bar2,
		["realBar"] = MultiBarBottomLeft,
		["barNum"] = 2,
	}
	bars[2] = {
		["panel"] = caelPanel7,
		["button"] = "MultiBarBottomRightButton",
		["barFrame"] = bar3,
		["realBar"] = MultiBarBottomRight,
		["barNum"] = 3,
	}
	bars[3] = {
		["panel"] = caelPanel4,
		["button"] = "MultiBarRightButton",
		["barFrame"] = bar4,
		["realBar"] = MultiBarRight,
		["barNum"] = 4,
	}
	bars[4] = {
		["panel"] = caelPanel11,
		["button"] = "MultiBarLeftButton",
		["barFrame"] = bar5,
		["realBar"] = MultiBarLeft,
		["barNum"] = 5,
	}

	for _, bar in ipairs(bars) do
		local button1 = _G[bar.button .. "1"]

		-- Set the bar frame to the panel cael panel it is tied to.
		bar.barFrame:SetAllPoints(bar.panel)

		-- Set the Blizzard bar parent.
		bar.realBar:SetParent(bar.barFrame)

		for index = 1, 12 do
			local button = _G[bar.button .. index]
			local buttonPrev = _G[bar.button .. index - 1]

			button:ClearAllPoints()
			button:SetScale(0.68) -- 0.68625

			if index == 1 then
				button:SetPoint("TOPLEFT", bar.panel, pixelScale(4.5), pixelScale(-4.5))
			elseif bar.barNum == 5 then
				button:SetPoint("TOPLEFT", buttonPrev, "BOTTOMLEFT", 0, pixelScale(-4.5))
			elseif index == 7 then
				button:SetPoint("TOPLEFT", button1, "BOTTOMLEFT", 0, pixelScale(-6.5))
			else
				button:SetPoint("LEFT", buttonPrev, "RIGHT", pixelScale(5), 0)
			end

			-- mouse over enable
			if (actionBar["settings"]["showBar" .. bar.barNum] and actionBar["settings"]["mouseOverBar" .. bar.barNum]) then
				button:SetScript("OnEnter", function() mouseOverBar(bar.panel, bar.realBar, bar.button, 1) end)
				button:SetScript("OnLeave", function() mouseOverBar(bar.panel, bar.realBar, bar.button, 0) end)
				mouseOverBar(bar.panel, bar.realBar, bar.button, 0)
			end
		end

		if not actionBar["settings"]["showBar" .. bar.barNum] then
			bar.panel:Hide()
			bar.realBar:Hide()
			bar.barFrame:Hide()
		elseif actionBar["settings"]["mouseOverBar" .. bar.barNum] == true then
			bar.panel:EnableMouse(true)
			bar.panel:SetScript("OnEnter", function() mouseOverBar(bar.panel, bar.realBar, bar.button, 1) end)
			bar.panel:SetScript("OnLeave", function() mouseOverBar(bar.panel, bar.realBar, bar.button, 0) end)
			mouseOverBar(bar.panel, bar.realBar, bar.button, 0)
		end
	end
end

---------------------
-- EXTRA ACTION BAR
---------------------

local barExtra = CreateFrame("Frame", "barExtra", UIParent, "SecureHandlerStateTemplate")
barExtra:SetSize(30, 30)
barExtra:SetPoint("BOTTOM", caelPanel1, "TOP", 0, pixelScale(26))

ExtraActionBarFrame:SetParent(barExtra)
ExtraActionBarFrame:ClearAllPoints()
ExtraActionBarFrame:SetPoint("CENTER")
ExtraActionBarFrame.ignoreFramePositionManager = true

ExtraActionButton1:SetSize(30, 30)
barExtra.button = ExtraActionButton1

-------------------
-- SHAPESHIFT BAR
-------------------

local barShift = CreateFrame("Frame", "barShift", UIParent)
barShift:ClearAllPoints()
barShift:SetPoint("BOTTOMLEFT", bar1, "TOPLEFT",  pixelScale(3), pixelScale(-4))
barShift:SetWidth(29)
barShift:SetHeight(58)

-- Place buttons in the bar frame and set the barShift as the parent frame
StanceBarFrame:SetParent(barShift)
StanceBarFrame:SetWidth(0.00001)
for index = 1, NUM_STANCE_SLOTS do
	local button = _G["StanceButton" .. index]
	local buttonPrev = _G["StanceButton" .. index - 1]
	button:ClearAllPoints()
	button:SetScale(0.68) -- 0.68625
	if index == 1 then
		button:SetPoint("BOTTOMLEFT", barShift, 0, pixelScale(6.5))
	else
		button:SetPoint("LEFT", buttonPrev, "RIGHT", pixelScale(4.5), 0)
	end
end

-- Hook the updating of the shapeshift bar
local function MoveShapeshift()
	if not InCombatLockdown() then
		StanceButton1:SetPoint("BOTTOMLEFT", barShift, 0, pixelScale(6.5))
	end
end

hooksecurefunc("StanceBar_Update", MoveShapeshift)

------------
-- PET BAR
------------

-- Create pet bar frame and put it into place
local barPet = CreateFrame("Frame", "barPet", UIParent, "SecureHandlerStateTemplate")
barPet:ClearAllPoints()
barPet:SetWidth(pixelScale(120))
barPet:SetHeight(pixelScale(47))
barPet:SetPoint("BOTTOM", UIParent, pixelScale(-336.5), pixelScale(363.5))

-- Setup Blizzard pet action bar.
PetActionBarFrame:SetParent(barPet)
PetActionBarFrame:SetWidth(0.01)

-- Show grid for pet actionbar
if actionBar["settings"].showPetGrid == true then
	PetActionBar_ShowGrid()
end

-- function to toggle the display of the pet bar
local function togglePetBar(alpha)
	for index = 1, NUM_PET_ACTION_SLOTS do
		local button = _G["PetActionButton" .. index]
		button:SetAlpha(alpha)
	end
end

do
	local button1 = _G["PetActionButton1"]
	for index = 1, NUM_PET_ACTION_SLOTS do
		local button = _G["PetActionButton" .. index]
		local buttonPrev = _G["PetActionButton" .. index - 1]

		button:ClearAllPoints()

		-- Set Parent for position purposes
		button:SetParent(barPet)

		-- Set Scale for the button size.
		button:SetScale(0.68) 

		if index == 1 then
			button:SetPoint("TOPLEFT", barPet, pixelScale(4.5), pixelScale(-4.5))
		elseif index == ((NUM_PET_ACTION_SLOTS / 2) + 1) then -- Get our middle button + 1 to make the rows even
			button:SetPoint("TOPLEFT", button1, "BOTTOMLEFT", 0, pixelScale(-5))
		else
			button:SetPoint("LEFT", buttonPrev, "RIGHT", pixelScale(4.5), 0)
		end

		-- Toggle buttons if mouse over is turned on.
		if actionBar["settings"].mouseOverPetBar == true then
			button:SetAlpha(0)
			button:HookScript("OnEnter", function(self) togglePetBar(1) end)
			button:HookScript("OnLeave", function(self) togglePetBar(0) end)
		end
	end
end

-- Toggle pet bar if mouse over is turned on.
if actionBar["settings"].mouseOverPetBar == true then
	barPet:EnableMouse(true)
	barPet:SetScript("OnEnter", function(self) togglePetBar(1) end)
	barPet:SetScript("OnLeave", function(self) togglePetBar(0) end)
end

--------------
-- TOTEMS BAR
--------------
local totemBar = _G["MultiCastActionBarFrame"]

if totemBar then
	totemBar:SetScript("OnUpdate", nil)
	totemBar:SetScript("OnShow", nil)
	totemBar:SetScript("OnHide", nil)
	totemBar:SetParent(caelPanel3)
	totemBar:ClearAllPoints()
	totemBar:SetPoint("BOTTOM", caelPanel3, "TOP", 0, caelLib.scale(5))
	totemBar:SetScale(0.85)

	hooksecurefunc("MultiCastActionButton_Update", function(self)
		if not InCombatLockdown() then
			self:SetAllPoints(self.slotButton)
		end
	end)
end

------------
-- VEHICLE
------------

-- Vehicle button
local vehicleExitButton = CreateFrame("BUTTON", nil, UIParent, "SecureActionButtonTemplate")

vehicleExitButton:SetSize(pixelScale(33), pixelScale(33))
vehicleExitButton:SetPoint("BOTTOM", pixelScale(-146), pixelScale(263))

vehicleExitButton:RegisterForClicks("AnyUp")
vehicleExitButton:SetScript("OnClick", function() VehicleExit() end)

vehicleExitButton:SetNormalTexture([=[Interface\Vehicles\UI-Vehicles-Button-Exit-Up]=])
vehicleExitButton:SetPushedTexture([=[Interface\Vehicles\UI-Vehicles-Button-Exit-Down]=])
vehicleExitButton:SetHighlightTexture([=[Interface\Vehicles\UI-Vehicles-Button-Exit-Down]=])

kill(StanceBarLeft)
kill(StanceBarMiddle)
kill(StanceBarRight)

vehicleExitButton:RegisterEvent("UNIT_ENTERING_VEHICLE")
vehicleExitButton:RegisterEvent("UNIT_ENTERED_VEHICLE")
vehicleExitButton:RegisterEvent("UNIT_EXITING_VEHICLE")
vehicleExitButton:RegisterEvent("UNIT_EXITED_VEHICLE")
vehicleExitButton:RegisterEvent("ZONE_CHANGED_NEW_AREA")
vehicleExitButton:SetScript("OnEvent", function(self, event, arg1)
	if (((event == "UNIT_ENTERING_VEHICLE") or (event == "UNIT_ENTERED_VEHICLE"))
		and arg1 == "player") then
		vehicleExitButton:SetAlpha(1)
	elseif (
		(
		(event == "UNIT_EXITING_VEHICLE") or (event == "UNIT_EXITED_VEHICLE")
		) and
		arg1 == "player") or (
		event == "ZONE_CHANGED_NEW_AREA" and not UnitHasVehicleUI("player")
		) then
		vehicleExitButton:SetAlpha(0)
	end
end)

vehicleExitButton:SetAlpha(0)